package zhongbo.java.air.dictionary.service.ServiceImpl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.BusinessException;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.dictionary.mapper.AlertZoneMapper;
import zhongbo.java.air.dictionary.model.AlertZone;
import zhongbo.java.air.dictionary.service.IAlertZoneService;

import java.util.Date;

@Service
public class IAlertZoneServiceImpl extends ServiceImpl<AlertZoneMapper, AlertZone> implements IAlertZoneService {
    @Autowired
    private AlertZoneMapper alertZoneMapper;

    @Override
    public PageVo<AlertZone> pageQuery(Integer page, Integer pageSize
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<AlertZone> queryWrapper = new QueryWrapper<AlertZone>();
        Page<AlertZone> alertZonePage = new Page<>(page,pageSize);
        if (oderByField == null || oderByField.isEmpty()){
            queryWrapper.orderByAsc("update_time");
        }else {
            if (orderByAsc){
                queryWrapper.orderByAsc(oderByField);
            }else {
                queryWrapper.orderByDesc(oderByField);
            }
        }

        //指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)){
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        Page<AlertZone> alertZonePage1 = alertZoneMapper.selectPage(alertZonePage, queryWrapper);
        return new PageVo<>(alertZonePage1);
    }

    @Override
    public ResultVo saveAlertZone(AlertZone alertZone) {
        alertZone.setCreateTime(new Date());
        alertZone.setUpdateTime(new Date());
        alertZone.setEffectiveStart(new Date());

        if (alertZone.getZoneCode() == null || alertZone.getZoneCode().isEmpty()){
            throw new BusinessException("区域业务编号不能为空");
        }else if (alertZone.getZoneCode().length() > 20){
            throw new BusinessException("区域业务编号长度不能大于20");
        }

        if (alertZone.getZoneType() == null || alertZone.getZoneType().isEmpty()) {
            throw new BusinessException("区域类型不能为空");
        }

        if (alertZone.getVertices() == null || alertZone.getVertices() .isEmpty()){
            throw new BusinessException("多边形顶点坐标不能为空");
        }

        LambdaQueryWrapper<AlertZone> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlertZone::getZoneCode,alertZone.getZoneCode());
        if (!alertZoneMapper.selectList(queryWrapper).isEmpty()){
            throw new BusinessException("区域业务编号只能是唯一编号");
        }

        if (!(alertZoneMapper.insert(alertZone) > 0)){
            return ResultVo.error("添加失败");
        }
        return ResultVo.ok("添加成功");
    }

    @Override
    public ResultVo updateAlertZone(AlertZone alertZone) {
        alertZone.setUpdateTime(new Date());
        if (alertZone.getId() == null || alertZone.getId().isEmpty()){
            return ResultVo.error("id不能为空");
        }

        LambdaQueryWrapper<AlertZone> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlertZone::getZoneCode,alertZone.getZoneCode());
        if (!alertZoneMapper.selectList(queryWrapper).isEmpty()){
            throw new BusinessException("区域业务编号只能是唯一编号");
        }

        if (!(alertZoneMapper.updateById(alertZone) > 0)){
            return ResultVo.error("修改失败,不存在");
        }
        return ResultVo.success();
    }
}
